require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'select2': '/js/lib/select2.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery'],
        'paginator': ['bootstrap']
    }
});

var myFun = {};

require(['jquery', 'doT', 'select2', 'common', 'bootstrap', 'paginator'], function ($, doT, select2, common) {
    $(function () {
        $(document).ajaxStart(function () {
            $('#tips').show();
        }).ajaxStop(function () {
            $('#tips').hide();
        });

        var pageSize = 10;
        var role = sessionStorage.getItem('role');
        var school_id = sessionStorage.getItem('school_id');

        $('#school').on('change', function () {
            refreshCanteen(1);
        });

        function initSchool() {

            $.get(common.urlBase + "/school/allSchool.json", function (data, statu) {
                data = eval('(' + data + ')');
                var schools = data.data;
                myFun.schoolList = data.data;
                var schoolTmpl = doT.template($('#template_school').text());
                $('#school').html(schoolTmpl(schools));
                $('#school').select2();
                if (role != 1) {
                    $('#school').attr('disabled', true);
                    $('#school').val(school_id);
                    $('#select2-school-container').text($('#school option[value=' + school_id + ']').text());
                }

                refreshCanteen(1);
            });
        }

        initSchool();

        //获取配送员数据
        function getCanteen(page, pageSize) {
            var schoolId = sessionStorage.getItem('school_id');

            if(role==1){
                schoolId = $('#school').val();
            }

            var canteensList = new Array();
            var canteenTmpl = doT.template($('#template_canteen').text());

            $.get(common.urlBase + '/sender/get.json?school_id='+schoolId, function (data, statu) {

                var data = eval('(' + data + ')');
                $('#paginator_canteen').hide();
                $('#canteen').html('');
                if (data.ok) {
                    console.log(data)
                    var total = data.data.total_result;
                    $('#total').html(total);
                    var pageNumber = Math.ceil(total / pageSize);
                    initPaginator(page, 8, pageNumber);
                    $('#table_canteen').show();
                    canteensList = data.data.obj;
                    $.each(canteensList, function (index, value) {
                        $.each(myFun.schoolList, function () {
                            if (canteensList[index].school_id == this.id) {
                                canteensList[index].school_name = this.name;
                            }
                        });
                    });
                    $('#canteen').html(canteenTmpl(canteensList));
                } else {
                    $('#table_canteen').hide();
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');

                }
            });

        }

        function refreshCanteen(page) {
            getCanteen(page, pageSize);
        }


        //初始化分页组件
        function initPaginator(currentPage, numberOfPages, totalPages) {
            var options = {
                currentPage: currentPage,
                numberOfPages: numberOfPages,
                totalPages: totalPages,
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case 'first':
                            return '首页';
                        case 'prev':
                            return '上一页';
                        case 'next':
                            return '下一页';
                        case 'last':
                            return '末页';
                        case 'page':
                            return page;

                    }
                },
                onPageChanged: function (event, oldPage, newPage) {
                    refreshCanteen(newPage);
                }
            };
            $('#paginator').bootstrapPaginator(options);
        }


        myFun.deleteCanteen = function deleteAdmin(obj) {
            var id = $(obj).data('target'); //获得按钮对应的条目信息
            if (confirm('是否确认删除？')) {
                $.get('http://tin168.com/packer/shop/deleteShop/' + id + '.json', function (data, statu) {
                    data = eval('(' + data + ')')
                    if (data['ok']) {
                        location.reload();
                    } else {
                        $('#noticeMsg').text(data['message']);
                        $('#noticeModal').modal('show');
                    }
                });
            }
        }
        myFun.changeCanteen = function changeCanteen(obj) {
            var id = $(obj).data('target');
            $(obj).attr('href', '/views/change_canteen.html?id=' + id);
        }

    });

});

